package com.crime.crimemonitorclient;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.crimemonitor.library.CrimeSummary;

public class CrimeToplistChartViewFragment extends CrimeChartViewFragment
		implements OnCrimeItemSelectedListener
{
	private final int	CRIME_PAGE_SIZE		= 10;
	private final int	mDisplayCrimesFrom	= 0;
	private int			mDisplayCrimesTo = -1;
	private Button		mLoadNextCrimeItemsButton;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		if(savedInstanceState != null)
		{
			// obnov pocet aktualne zobrazenych trestnych cinu v grafu
			mDisplayCrimesTo = savedInstanceState.getInt("mDisplayCrimesTo");
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);

		// uloz si pocet aktualne zobrazenych trestnych cinu v grafu
		outState.putInt("mDisplayCrimesTo", mDisplayCrimesTo);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		View view = super.onCreateView(inflater, container, savedInstanceState);

		mLoadNextCrimeItemsButton = (Button) view
				.findViewById(R.id.crime_toplist_fragment_load_next);
		mLoadNextCrimeItemsButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View button)
			{
				CrimeToplistChartViewFragment.this.loadNextCrimeItems(button);
			}
		});

		return view;
	}

	@Override
	protected void setupWebView()
	{
		super.setupWebView();

		mWebView.addJavascriptInterface(new CrimeSummaryGraphWebAppInterface(
				this), "ClickListener");
	}

	@Override
	protected void initBeforeCreatingChart()
	{
		super.initBeforeCreatingChart();
		if(mDisplayCrimesTo == -1)
			mDisplayCrimesTo = CRIME_PAGE_SIZE;
		else
			mDisplayCrimesTo = Math.min(mCrimesSummary.getCrimeSummaries().length,
					mDisplayCrimesTo);
	}

	@Override
	protected String getContentFromTemplate()
	{
		// inject values to a crime summary HTML page
		StringBuilder dataSourceSb = new StringBuilder();
		// String eol = System.getProperty("line.separator");
		CrimeSummary crimeSummaries[] = mCrimesSummary.getCrimeSummaries();
		for (int i = mDisplayCrimesFrom; i < mDisplayCrimesTo; ++i)
		{
			CrimeSummary crimeSummary = crimeSummaries[i];
			dataSourceSb.append(this
					.getCrimeSummaryChartRowDefinitionForCrime(crimeSummary));
		}

		String dataDefinitionString = dataSourceSb.toString();

		final Float crimeSummaryItemHeightInDip = 50.0f;
		Float graphCSSHeight = (mDisplayCrimesTo - mDisplayCrimesFrom)
				* crimeSummaryItemHeightInDip;

		return mContentTemplate
				.replace("%dataDefinition", dataDefinitionString).replace(
						"%height", graphCSSHeight.toString());
	}

	private String getCrimeSummaryChartRowDefinitionForCrime(
			CrimeSummary crimeSummary)
	{
		return String.format("data.addRow(['%s', %d]);",
				crimeSummary.getCrimeName(), crimeSummary.getCrimeNumber());
	}

	@Override
	protected String getTitle()
	{
		return this.getResources().getString(
				R.string.crime_chartview_fragment_toplist_label);
	}

	@Override
	protected int getFragmentLayoutId()
	{
		return R.layout.crime_toplist_fragment_layout;
	}

	public void loadNextCrimeItems(View button)
	{
		mDisplayCrimesTo = Math.min(mCrimesSummary.getCrimeSummaries().length,
				mDisplayCrimesTo + CRIME_PAGE_SIZE);
		this.redraw();
	}

	@Override
	protected void updateUIBeforeChartLoad()
	{
		super.updateUIBeforeChartLoad();
		mLoadNextCrimeItemsButton.setVisibility(View.GONE);
	}

	@Override
	protected void updateUIAfterChartLoad()
	{
		super.updateUIAfterChartLoad();

		// uprav zobrazeni tlacitka pro nahravani dalsich dat
		this.mLoadNextCrimeItemsButton
				.setVisibility(mDisplayCrimesTo == mCrimesSummary
						.getCrimeSummaries().length ? View.GONE : View.VISIBLE);
	}

	@Override
	public void onCrimeItemSelected(int crimeItemIndex)
	{
		CrimeSummary selectedCrimeSummary = mCrimesSummary.getCrimeSummaries()[crimeItemIndex];
		Bundle crimeDetailBundle = new Bundle();
		crimeDetailBundle.putInt("tsk", selectedCrimeSummary.getTsk());
		crimeDetailBundle.putLong("startDate",
				mCrimesSummary.getStartDateInMillis());
		crimeDetailBundle.putLong("endDate",
				mCrimesSummary.getEndDateInMillis());

		Intent displayCrimeDetail = new Intent(this.getActivity(),
				DisplayCrimeDetailActivity.class);

		displayCrimeDetail.putExtras(crimeDetailBundle);

		this.startActivity(displayCrimeDetail);
	}
}
